package net.coding.li709.materialrefresher.manage;

import com.google.common.reflect.TypeToken;
import net.coding.li709.materialrefresher.MaterialRefresher;
import net.coding.li709.materialrefresher.keys.FlanData;
import net.coding.li709.materialrefresher.model.SpawnLoc;
import net.coding.li709.serverbase.utils.ConfigUtils;
import ninja.leaping.configurate.ConfigurationNode;
import ninja.leaping.configurate.objectmapping.ObjectMappingException;
import org.spongepowered.api.scheduler.Task;

import java.util.HashMap;
import java.util.Map;

public class BoxSpawnManager {

    private static Map<String, SpawnLoc> map=new HashMap<>();
    private static Task task;


    public static void init(){
        BoxSpawnManager.task = Task.builder().intervalTicks(1).execute(BoxSpawnManager::onUpdate).submit(MaterialRefresher.getInstance());
    }
    /**
     * 加载各个世界的生成点配置
     */
    public static void loadWorldSpawn(String worldName) {
        ConfigurationNode spawnLocConfig = ConfigUtils.loadConfig(MaterialRefresher.ID,"spawnLoc",false);
        try {
            for (Object o : spawnLocConfig.getChildrenMap().keySet()) {
                if (o.toString().equals("isLoadDBConfig")){
                    continue;
                }
                SpawnLoc value = spawnLocConfig.getNode(o.toString()).getValue(TypeToken.of(SpawnLoc.class));
                if (worldName.equals(value.getWorld())){
                    value.init();
                    map.put(value.getName(), value);
                }
           }
        } catch (ObjectMappingException e) {
            e.printStackTrace();
        }
    }

    //服务器关闭保存状态到配置文件
    public static void stop() {
        task.cancel();
    }

    /**
     * 拾取生成点的物品
     */
    public static void pickupSpawnLoc(FlanData flanData) {
        if (flanData.getSpawnName().equals("")||!map.containsKey(flanData.getSpawnName())) {
            return;
        }
        map.get(flanData.getSpawnName()).reset();
    }

    /**
     * 添加生成点
     * @param spawnLoc
     */
    public static void addSpawnLoc(SpawnLoc spawnLoc){

        ConfigurationNode spawnLocConfig = ConfigUtils.loadConfig(MaterialRefresher.ID,"spawnLoc",false);
        try {
            spawnLocConfig.getNode(spawnLoc.getName()).setValue(TypeToken.of(SpawnLoc.class),spawnLoc);
            ConfigUtils.saveLocalConfig(MaterialRefresher.ID,"spawnLoc",spawnLocConfig);
        } catch (ObjectMappingException e) {
            e.printStackTrace();
        }

        map.put(spawnLoc.getName(),spawnLoc);
        spawnLoc.spawn();
    }

    /**
     * 生成点生成了道具，改为未拾取状态
     */
    public static void changeSpawnLocState(FlanData flanData) {
        if (flanData.getSpawnName().equals("")) {
            return;
        }
        map.get(flanData.getSpawnName()).changeSpawnLocState();
    }

    /**
     * 每0.1秒调用一次
     */
    private static void onUpdate() {
        for (SpawnLoc spawnLoc : map.values()) {
            spawnLoc.onUpdate();
        }
    }

    /**
     * 使用宝箱
     */
    public static SpawnLoc getSpawnLoc(String name) {
        return map.get(name);
    }

    /**
     * 使用宝箱
     */
    public static boolean isSpawnLoc(String name) {
        return map.containsKey(name);
    }
}
